from PyQt4 import QtGui;
        
class AddNodeCommand(QtGui.QUndoCommand):
    def __init__(self, gNodes=None, graphicsScene=None):
        self.gNodes = gNodes;
        if len(gNodes) > 1:
            msgUndo = "Add Multiple Nodes";
        else:
            msgUndo = "Add Node";
        super(AddNodeCommand, self).__init__(msgUndo);
        self.graphicsScene = graphicsScene;
    
    def undo(self):
        for i in range(len(self.gNodes)): #@UnusedVariable
            self.graphicsScene.removeItem(self.gNodes[i]);
    
    def redo(self):
        for i in range(len(self.gNodes)): #@UnusedVariable
            self.graphicsScene.addItem(self.gNodes[i]);
        
        
class DeleteNodeCommand(QtGui.QUndoCommand):
    def __init__(self, gSelectedNodes=None, graphicsScene=None):
        self.selectedNodes = gSelectedNodes;
        self.allInEdges = [];
        self.allOutEdges = [];
        for selectedNode in gSelectedNodes:
            self.allInEdges.append(selectedNode.getInEdges());
            self.allOutEdges.append(selectedNode.getOutEdges());
        if len(gSelectedNodes) > 1:
            msgUndo = "Delete Multiple Nodes";
        else:
            msgUndo = "Delete Node";
        super(DeleteNodeCommand, self).__init__(msgUndo);
        self.graphicsScene = graphicsScene;
    
    def undo(self):
        for inEdges in self.allInEdges:
            for inEdge in inEdges:
                self.graphicsScene.addItem(inEdge);
        for outEdges in self.allOutEdges:
            for outEdge in outEdges:
                if self.getRepeats(outEdge)==0:
                    self.graphicsScene.addItem(outEdge);
        for i in range(len(self.selectedNodes)):
            self.graphicsScene.addItem(self.selectedNodes[i]);
            self.selectedNodes[i].addAllEdges(self.allInEdges[i], self.allOutEdges[i]);
    
    def redo(self):
        for inEdges in self.allInEdges:
            for inEdge in inEdges:
                self.graphicsScene.removeItem(inEdge);
        for outEdges in self.allOutEdges:
            for outEdge in outEdges:
                if self.getRepeats(outEdge)==0:
                    self.graphicsScene.removeItem(outEdge);
        for i in range(len(self.selectedNodes)-1,-1,-1):
            self.graphicsScene.removeItem(self.selectedNodes[i]);
            self.selectedNodes[i].removeAllEdges();
            
    def getRepeats(self, edge):
        nRepeats = 0;
        for inEdges in self.allInEdges:
            nRepeats += inEdges.count(edge);
        return nRepeats;
        

class MoveNodeCommand(QtGui.QUndoCommand):
    def __init__(self, gSelectedNodes=None, graphicsScene=None):
        self.oldPos = [];
        self.newPos = [];
        self.gSelectedNodes = gSelectedNodes;
        for i in range(len(self.gSelectedNodes)):
            self.oldPos.append(self.gSelectedNodes[i].oldPos);
            self.newPos.append(self.gSelectedNodes[i].newPos);
        if len(gSelectedNodes) > 1:
            msgUndo = "Move Multiple Nodes";
        else:
            msgUndo = "Move Node to " + str(self.newPos[0].x()) + ", " + str(self.newPos[0].y());
        super(MoveNodeCommand, self).__init__(msgUndo);
        self.graphicsScene = graphicsScene;
    
    def undo(self):
        for i in range(len(self.gSelectedNodes)):
            self.gSelectedNodes[i].setPos(self.oldPos[i]);
        self.graphicsScene.showNodeClusters();
    
    def redo(self):
        for i in range(len(self.gSelectedNodes)):
            self.gSelectedNodes[i].setPos(self.newPos[i]);
        self.graphicsScene.showNodeClusters();
